home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / ENTRPRIS / APE / AELOGGER / MODLOGGR.BAS < prev   
Encoding:
BASIC Source File  |  1996-12-06  |  14.0 KB  |  306 lines

  1. Attribute VB_Name = "modLogger"
  2. Option Explicit
  3. '-------------------------------------------------------------------------
  4. 'The project is the Logger component of the Application Performance Explorer
  5. 'The Logger is a multiuse server that objects can call to pass log records
  6. 'The logger will store the records, either in memory or in a temp file.
  7. 'The logger will then return the records the the Manager when it calls GetRecords
  8. '
  9. 'Key Files:
  10. '   frmLoggr.frm    Only form in app
  11. '   clsPosFm.cls    Tool used to save form position in registry
  12. '   Logger.cls      Multi-Use public class providing only OLE interface
  13. '-------------------------------------------------------------------------
  14.  
  15. 'API Declares
  16. #If UNICODE Then
  17.     Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameW" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
  18.     Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathW" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
  19. #Else
  20.     Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
  21.     Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
  22. #End If
  23. Declare Function GetTickCount Lib "kernel32" () As Long
  24.  
  25. 'Public Constants
  26. Public Const glROWS_RETURNED_PER_GET_RECORDS As Long = 500  'Max number of records returned for
  27.                                                         'each call of GetRecords
  28. 'Property Defaults
  29. Public Const gbSHOW_FORM_DEFAULT As Boolean = False
  30. Public Const gbWRITE_RECORDS_DEFAULT As Boolean = False
  31. Public Const gbTHRESHOLD_DEFAULT As Long = 2000
  32. Public Const glREDIM_CHUNK_SIZE As Long = 100
  33. Public Const giNO_RECORDS As Integer = -1
  34.  
  35. 'Resource string constants
  36. Public Const giLOGGER_NAME As Integer = 2
  37. Public Const giDISK_FULL As Integer = 3
  38. Public Const giWRITING_TEMP_FILE As Integer = 4
  39. Public Const giFORM_CAPTION As Integer = 5
  40. Public Const giFONT_CHARSET_INDEX As Integer = 30
  41. Public Const giFONT_NAME_INDEX As Integer = 31
  42. Public Const giFONT_SIZE_INDEX  As Integer = 32
  43.  
  44. 'Global Variables
  45. Public gbShowForm As Boolean        'If true show form
  46. Public gbWriteRecords As Boolean    'If true write records to file when Record
  47.                                     'Threshold is reached.
  48. Public glThreshold As Long          'Record threshold in kilobytes
  49. Public glThresholdRecs As Long      'Record threshold in number of records
  50. Public gsFileName As String         'FileName to write records to
  51. Public gaRecords() As Variant       'Array used to store log records before they are written
  52. Public glInstances As Long          'Counter of how many instances of Logger are instanciated
  53. Public glLastAddedRecord As Long    'Last index of gaRecords that a record was added to
  54. Public gbWritingFile As Boolean     'If true we are in WriteRecords procedure
  55. Public gbDiskFull As Boolean        'If true Disk Full error occured
  56. Public gbGetWrittenLogCalled As Boolean 'Get Written Log has been called by Manager
  57.                                         'Now logger is expecting GetWrittenLog to be called
  58.                                         'until all records are received.  The next time a record
  59.                                         'is written the temp file will be deleated assuming that all
  60.                                         'records were received.
  61.  
  62.  
  63. Sub Main()
  64. End Sub
  65.  
  66. Public Sub WriteRecords()
  67.     '-------------------------------------------------------------------------
  68.     'Purpose:   WriterRecords procedure writes all the log records currently
  69.     '           in the global array
  70.     'Effects:
  71.     '           [gbGetWrittenLogCalled] becomes false
  72.     '           The temp file is deleted if gbGetWrittenLogCalled is true
  73.     '           [glLastAddedRecord] is set to giNO_RECORDS
  74.     '           [gaRecords]is redimensioned to glREDIM_CHUNK_SIZE
  75.     'Assumption:
  76.     '           gsFileName is a valid temporary file name
  77.     '           If gbGetWrittenLogCalled is true then all the records in
  78.     '               the temp file have been retrieved by the manager through
  79.     '               the GetRecords method
  80.     '-------------------------------------------------------------------------
  81.     
  82.     Dim iFile As Integer        'File number
  83.     Dim l As Long               'For...Next counter
  84.     Dim sComponent As String    'APE Component name being written
  85.     Dim lServiceID As Long      'Service ID (Task request ID) being written
  86.     Dim sComment As String      'Comment being written
  87.     Dim lMilliseconds As Long   'Milliseconds being written
  88.     
  89.     On Error GoTo WriteRecordsError
  90.     
  91.     'Check to see if the contents of the temp file
  92.     'need deleted first, the reason it is not delete
  93.     'when the flag is flipped is to give one the chance
  94.     'of rescueing it if the Manager fails to retreive
  95.     'the records from it
  96.     If gbGetWrittenLogCalled Then
  97.         Close   'Close in case Getting log was cancelled
  98.         Kill gsFileName
  99.         gbGetWrittenLogCalled = False
  100.     End If
  101.     
  102.     If glLastAddedRecord > giNO_RECORDS Then
  103.         AddLogRecord LoadResString(giLOGGER_NAME), 0, LoadResString(giWRITING_TEMP_FILE), GetTickCount
  104.         iFile = FreeFile
  105.         Open gsFileName For Append As iFile
  106.         'Iterate through array writing record and
  107.         For l = 0 To glLastAddedRecord
  108.             sComponent = gaRecords(giCOMPONENT_ELEMENT, l)
  109.             lServiceID = gaRecords(giSERVICE_ELEMENT, l)
  110.             sComment = gaRecords(giCOMMENT_ELEMENT, l)
  111.             lMilliseconds = gaRecords(giMILLI_SECONDS_ELEMENT, l)
  112.             Write #iFile, sComponent, lServiceID, sComment, lMilliseconds
  113.             'Reset logrecord counter no after writing the first record
  114.             'so that records are not added after the count that is being
  115.             'written and therefore, lost.  This also protects from
  116.             'Addlogrecord trying to write a record greater than
  117.             'giRedimChunkSize write after gaRecords is redimensioned
  118.             If l = 0 Then glLastAddedRecord = giNO_RECORDS
  119.         Next
  120.         Close iFile
  121.         'Redimension array
  122.         'Preserve is used because there is a potential
  123.         'for a log record to be added after the above line
  124.         'but before the following one
  125.         ReDim Preserve gaRecords(giLOG_ARRAY_DIMENSION_ONE, glREDIM_CHUNK_SIZE)
  126.     End If
  127.     
  128.     Exit Sub
  129. WriteRecordsError:
  130.     Select Case Err.Number
  131.         Case ERR_DISK_FULL
  132.             'Turn off logging erase array
  133.             'leave present file for later retrieval
  134.             DisplayStatus LoadResString(giDISK_FULL)
  135.             Close iFile
  136.             Erase gaRecords
  137.             gbDiskFull = True
  138.             Exit Sub
  139.         Case ERR_FILE_NOT_FOUND
  140.             'There is no temp file to kill
  141.             Resume Next
  142.         Case Else
  143.             Close iFile
  144.             Err.Raise Err.Number, Err.Source, Err.Description
  145.             Exit Sub
  146.     End Select
  147. End Sub
  148.  
  149. Public Sub GetWrittenLog()
  150.     '-------------------------------------------------------------------------
  151.     'Purpose:   Checks to see if there is log records written to a temp file
  152.     '           If there are it inputs it and adds it to the gaRecords array
  153.     '           If it reaches the chunk size for passing log records it will
  154.     '           exit the loop, leaving the file open. It is necessary to keep
  155.     '           calling this function until no records or added.  Do not call
  156.     '           this function more than once until the array that was filled
  157.     '           was erased.  The external process that is calling a method that
  158.     '           calls this procedure should be responsible for calling until
  159.     '           all records have been attained.
  160.     'Effects:
  161.     '           [gbGetWrittenLogCalled] becomes true
  162.     '           Temp file may be left open if all records are not read
  163.     '           AddlogRecord is called for each record read
  164.     'Assumption:
  165.     '           If gbGetWrittenLogCalled is true then the temp file is already
  166.     '           open, ready for the next record to be read.
  167.     '           If the EOF is not reached before the glROWS_RETURNED_PER_GET_RECORDS
  168.     '           is reached then the external process that called Logger.GetRecords
  169.     '           will call it again, to get the rest of the records
  170.     '-------------------------------------------------------------------------
  171.     
  172.     Static stlFile As Long      'File number of file that may be left open between calls
  173.     Dim sComponent As String    'APE Component name that will be read from file
  174.     Dim lServiceID As Long      'Service ID that will be read from file
  175.     Dim sComment As String      'Comment that will be read from file
  176.     Dim lMilliseconds As Long   'Milliseconds that will be read from file
  177.     Dim lAddedCount As Long     'Used to count how many records have been read and
  178.                                 'added to global array
  179.     
  180.     On Error GoTo GetWrittenLogError
  181.     'Open file if not open yet
  182.     If Not gbGetWrittenLogCalled Then
  183.         'Write records in memory first to order the records
  184.         'with any records that may have already been written
  185.         WriteRecords
  186.         gbGetWrittenLogCalled = True
  187.         stlFile = FreeFile
  188.         Open gsFileName For Input As stlFile
  189.     End If
  190.     
  191.     
  192.     Do Until EOF(stlFile)
  193.         Input #stlFile, sComponent, lServiceID, sComment, lMilliseconds
  194.         AddLogRecord sComponent, lServiceID, sComment, lMilliseconds
  195.         lAddedCount = lAddedCount + 1
  196.         'Exit here if max record size was reached
  197.         If lAddedCount = glROWS_RETURNED_PER_GET_RECORDS Then Exit Sub
  198.     Loop
  199.     Close
  200.     Exit Sub
  201. GetWrittenLogError:
  202.     Select Case Err.Number
  203.         Case ERR_FILE_NOT_FOUND
  204.             'There are no written records so exit without calling gSendLog
  205.             Exit Sub
  206.         Case ERR_BAD_FILE_NAME
  207.             'We have already reached the end of the file
  208.             'and it has been closed
  209.             Exit Sub
  210.         Case ERR_IPUT_PAST_EOF
  211.             'This could occur if a temp file was artificially made that
  212.             'had an invalid format
  213.             Close stlFile
  214.             Exit Sub
  215.         Case Else
  216.             Close stlFile
  217.             Err.Raise Err.Number, Err.Source, Err.Description
  218.             Exit Sub
  219.     End Select
  220. End Sub
  221.  
  222. Public Sub AddLogRecord(sComponent As String, lServiceID As Long, sComment As String, lMilliseconds As Long)
  223.     '-------------------------------------------------------------------------
  224.     'Purpose:   Called to add a record to the gaRecords.
  225.     'In:        [sComponent]    APE component name that will be added
  226.     '           [lServiceID]    Service ID that will be added
  227.     '           [sComment]      Comment that will be added
  228.     '           [lMilliseconds] Milliseconds that will be added
  229.     'Effects:   [gaRecords]     May be redimensioned (preserve) to increase
  230.     '                           its size
  231.     '           [glLastAddedRecord]
  232.     '                           will be increased by one
  233.     '-------------------------------------------------------------------------
  234.     Dim lU As Long      'The UBound of the the 2nd dimension of gaRecords
  235.     On Error GoTo AddLogRecordError
  236. AddLogRecordTop:
  237.     'If diskfull error occured immediately exit
  238.     If gbDiskFull Then Exit Sub
  239.     
  240.     If glLastAddedRecord = giNO_RECORDS Then
  241.         ReDim gaRecords(giLOG_ARRAY_DIMENSION_ONE, glREDIM_CHUNK_SIZE)
  242.         glLastAddedRecord = 0
  243.     Else
  244.         lU = UBound(gaRecords, 2)
  245.         glLastAddedRecord = glLastAddedRecord + 1
  246.         If glLastAddedRecord > lU Then
  247.             'Redim gaRecords to increase size
  248.             lU = lU + glREDIM_CHUNK_SIZE
  249.             ReDim Preserve gaRecords(giLOG_ARRAY_DIMENSION_ONE, lU)
  250.         End If
  251.     End If
  252.     gaRecords(giCOMPONENT_ELEMENT, glLastAddedRecord) = sComponent
  253.     gaRecords(giSERVICE_ELEMENT, glLastAddedRecord) = lServiceID
  254.     gaRecords(giCOMMENT_ELEMENT, glLastAddedRecord) = sComment
  255.     gaRecords(giMILLI_SECONDS_ELEMENT, glLastAddedRecord) = lMilliseconds
  256.     Exit Sub
  257. AddLogRecordError:
  258.     Select Case Err.Number
  259.         Case ERR_SUBSCRIPT_OUT_OF_RANGE
  260.             'Synchronicity issues caused this
  261.             'Got the glLastAddedRecord write before it got changed
  262.             'but tried to put record in array right after it got redim'ed
  263.             Dim bTried
  264.             'If already tried raise error
  265.             If bTried Then Err.Raise Err.Number, Err.Source, Err.Description
  266.             bTried = True
  267.             'Try the at the top again, getting a new glLastAddedRecord
  268.             GoTo AddLogRecordTop
  269.         Case Else
  270.             Err.Raise Err.Number, Err.Source, Err.Description
  271.     End Select
  272. End Sub
  273.  
  274. 'Puts a message in the status label
  275. Public Sub DisplayStatus(s As String)
  276.     '-------------------------------------------------------------------------
  277.     'Purpose:   Displays passed string in the Logger form's status box if
  278.     '           the form is visible.
  279.     'Assumtions:
  280.     '           If gbShowForm is true the form is loaded and visible
  281.     '-------------------------------------------------------------------------
  282.     If gbShowForm Then frmLogger.lblStatus = s
  283. End Sub
  284.  
  285. Public Function GetTempFile() As String
  286.     '-------------------------------------------------------------------------
  287.     'Purpose:   Gets a temp file name from the system
  288.     'Return:    a valid temporary file name
  289.     '-------------------------------------------------------------------------
  290.     Dim lSize As Long
  291.     Dim sPath As String
  292.     Dim sName As String
  293.     Dim lResult As Long
  294.     
  295.     sPath = Space(255)
  296.     lResult = GetTempPath(255, sPath)
  297.     sPath = Left$(sPath, lResult)
  298.     sName = Space(255)
  299.     lResult = GetTempFileName(sPath, "AEL", 0, sName)
  300.     lResult = InStr(sName, vbNullChar)
  301.     sName = Left$(sName, lResult - 1)
  302.     
  303.     GetTempFile = sName
  304. End Function
  305.  
  306.